package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.ErrorCode;
import com.visizen.wisdomclass.server.action.response.ErrorCodeManager;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.service.UserService;
import com.visizen.wisdomclass.server.token.WCToken;
import com.visizen.wisdomclass.server.token.WCTokenManager;
import com.visizen.wisdomclass.server.util.WCDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 【用户】 修改用户信息
 *
 * @author: chenjian
 */
@Controller
@RequestMapping("/User")
public class UserChangeUserInfoAction {
    /**
     * 修改用户个人信息，根据token获取对应用户，所有参数都是可选的，哪一个参数存在就执行对应的修改，不存在不执行
     *
     * @param token
     * @param attachId    用户的附件ID
     * @param birthday    用户的生日
     * @param mobilephone 用户的手机号码
     * @param email       用户的邮箱
     * @param request
     * @return
     */
    @ValidateToken
    @RequestMapping(value = "/changeUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result changeUserInfo(@RequestParam(required = true) String token,
                                 @RequestParam(required = false) Long attachId,
                                 @RequestParam(required = false) String birthday,
                                 @RequestParam(required = false) String mobilephone,
                                 @RequestParam(required = false) String email,
                                 HttpServletRequest request) {
        Result result = new Result();
        JSONObject jo = new JSONObject();
        WCToken wcToken = tokenManager.getToken(token);

        WcUser user = (WcUser) wcToken.getSource();
        if (null != attachId && 0 != attachId)
            user.setAttach_id(attachId);
        if (null != birthday && !"".equals(birthday))
            user.setBirthday(WCDate.toDate(birthday));
        if (null != mobilephone && !"".equals(mobilephone))
            user.setMobilephone(mobilephone);
        if (null != email && !"".equals(email))
            user.setEmail(email);
        /**
         * 往数据库中更新用户的信息
         */
        boolean isSucc = userService.updateUserInfo(user);
        /**
         * 判断是否更新成功
         */
        if (!isSucc) {
            /**
             * 更新失败
             */
            jo.put("exception", "");
            ErrorCodeManager codeManager = ErrorCodeManager.getLocalMsg(ErrorCode.C1023_changeUserInfoFailed);
            result.setFailedResult(request, codeManager.getCode(), codeManager.getMsg(), jo);
        } else {
            /**
             * 更新用户信息成功后。
             * 从数据库中获取最新的用户信息，用户更新token中的用户信息
             */
            WcUser newUser = userService.getUserByUsername(user.getUsername());
            wcToken.setSource(newUser);
            tokenManager.updateToken(token, wcToken);
            result.setSuccessResult(request, "", null);
        }
        return result;
    }

    private WCTokenManager tokenManager;
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setTokenManager(WCTokenManager tokenManager) {
        this.tokenManager = tokenManager;
    }
}
